/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.comut.crud.ejb;

import com.comut.crud.bo.FleetHandler;
import com.comut.crud.connection.IFleetConnectionManager;
import com.comut.crud.connection.MooveFleetConnectionManager;
import com.comut.crud.ejb.interfaces.local.IFleetActionLocal;
import com.comut.crud.ejb.interfaces.remote.IFleetActionRemote;
import com.comut.crud.pojo.fleet.Fleet;
import com.comut.lib.error.ErrorCodes;
import com.comut.lib.exception.BaseException;
import java.sql.Connection;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;

/**
 *
 * @author asantos
 */
@Stateless
public class FleetBean implements IFleetActionLocal, IFleetActionRemote {
    private static Logger _logger = Logger.getLogger(FleetBean.class.getName());

    @PostConstruct
    private void fleetPostConstruct() {
        _logger.info("PostConstruct");
    }

    @PreDestroy
    private void fleetPreDestroy() {
        _logger.info("PreDestroy");
    }

    public FleetBean() {
    }

    public Fleet insertFleet(Fleet fleet) {
        Connection mooveFleetConnection = MooveFleetConnectionManager.instance().getConnection();
        Connection iFleetConnection = IFleetConnectionManager.instance().getConnection();
        FleetHandler th;
        Fleet toRet;
        try {
            th = new FleetHandler(mooveFleetConnection, iFleetConnection);
            toRet = th.insertFleet(fleet);
        } 
        finally{
            MooveFleetConnectionManager.instance().freeConnection(mooveFleetConnection);
            IFleetConnectionManager.instance().freeConnection(iFleetConnection);
            th = null;
        }
        return toRet;
    }

    public void deleteFleet(Fleet fleet) {
        if(fleet.getId()<1)
            throw new BaseException("The id must be a int bigger than 0", ErrorCodes.INVALID_ID);
        Connection mooveFleetConnection = MooveFleetConnectionManager.instance().getConnection();
        Connection iFleetConnection = IFleetConnectionManager.instance().getConnection();
        FleetHandler th;
        try {
            th = new FleetHandler(mooveFleetConnection, iFleetConnection);
            th.deleteFleet(fleet);
        }  
        finally{
            MooveFleetConnectionManager.instance().freeConnection(mooveFleetConnection);
            IFleetConnectionManager.instance().freeConnection(iFleetConnection);
            th = null;
        }
    }

    public void updateFleet(String fleet) {
        Connection mooveFleetConnection = MooveFleetConnectionManager.instance().getConnection();
        Connection iFleetConnection = IFleetConnectionManager.instance().getConnection();
        FleetHandler th;
        try {
            th = new FleetHandler(mooveFleetConnection, iFleetConnection);
            th.updateFleet(fleet);
        }  
        finally{
            MooveFleetConnectionManager.instance().freeConnection(mooveFleetConnection);
            IFleetConnectionManager.instance().freeConnection(iFleetConnection);
            th = null;
        }
    }

    public Fleet getFleet(Fleet fleet) {
        Connection mooveFleetConnection = MooveFleetConnectionManager.instance().getConnection();
        Connection iFleetConnection = IFleetConnectionManager.instance().getConnection();
        Fleet toRet;
        FleetHandler th;
        try {
            th = new FleetHandler(mooveFleetConnection, iFleetConnection);
            toRet = th.getFleet(fleet);
        }  
        finally{
            MooveFleetConnectionManager.instance().freeConnection(mooveFleetConnection);
            IFleetConnectionManager.instance().freeConnection(iFleetConnection);
            th = null;
        }
        return toRet;
    }
    
}
